package com.ctg.behavior.calc.impala.calculator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.api.CtCalcStatus;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtCalculator;
import com.ctg.behavior.calc.impala.builder.ltv.CtLTVCalcResultBuilder;
import com.ctg.behavior.calc.impala.builder.ltv.CtLTVReportBuilder;
import com.ctg.behavior.calc.impala.engine.ImpalaSQLEngine;
import com.ctg.behavior.calc.impala.report.ltv.CtLTVReport;
import com.ctg.behavior.calc.impala.transfer.ltv.CtLTVTransfer;
import com.ctg.behavior.calc.impala.utils.CtCalculatorUtils;
import com.ctg.behavior.client.impala.common.CtQueryResult;
import com.ctg.behavior.common.calc.CtCalcBody;
import com.ctg.behavior.common.calc.CtCalcResult;
import com.ctg.behavior.common.consts.CtCalcTaskConsts;
import com.ctg.behavior.common.ltv.CtLTVCalcBody;
import com.ctg.behavior.common.ltv.CtLTVCalcData;
import com.ctg.behavior.common.ltv.CtLTVCalcResult;
import com.ctg.behavior.utils.CtDateUtils;
import com.ctg.behavior.utils.CtStatusUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtLTVCalculator implements CtCalculator {

	@Autowired
	private CtLTVReportBuilder ltvReportBuilder;
	@Autowired
	private CtLTVTransfer ltvTransfer;
	@Autowired
	private CtLTVCalcResultBuilder ltvResultBuilder;

	@Autowired
	private ImpalaSQLEngine sqlEngine;

	/****************************************************************
	 * @apiNote: 1.
	 */
	@Override
	public CtCalcResult doCalculator(CtCalcBody calcBody) {
		CtLTVCalcResult calcResult = new CtLTVCalcResult();
		if (!(calcBody instanceof CtLTVCalcBody)) {
			log.error("calcBody={}", calcBody.getClass());
			CtStatusUtils.fillStatus(calcResult, CtCalcStatus.BodyNoMatch);
			return calcResult;
		}
		CtLTVCalcData calcData = new CtLTVCalcData();
		calcResult.setValue(calcData);

		CtLTVCalcBody ltvCalcBody = (CtLTVCalcBody) calcBody;
		log.info("doBuilder-start: {}", CtDateUtils.getCurrentDateSSS());
		CtLTVReport ltvReport = ltvReportBuilder.doBuilder(ltvCalcBody);
		log.info("doBuilder-end: {}", CtDateUtils.getCurrentDateSSS());

		// 2. 初始化ResultBuilder
		ltvResultBuilder.resetResultBuilderTheadLocal();
		// 3. 执行计算过程
		log.info("time - start: {}", CtDateUtils.getCurrentDateSSS());
		String ltvSQL = ltvTransfer.doTransfer(ltvReport, CtSQLParam.createParam());
		String queryType = CtCalcTaskConsts.TASK_TYPE_LTV;
		CtQueryResult queryResult = sqlEngine.doQuery(queryType, ltvSQL);
		log.info("queryResult={}", JSON.toJSONString(queryResult));
		
		CtCalculatorUtils.fillCalcResult(calcResult, queryResult);
		if (!calcResult.getSuccess()) {
			log.error(queryResult.getCode());
			return calcResult;
		}
		
		ltvResultBuilder.doBuilder(calcData, queryResult.getRows(), ltvReport);
		log.info("resultBuilder={}", JSON.toJSONString(calcData));
		log.info("time - end: {}", CtDateUtils.getCurrentDateSSS());

		return calcResult;
	}

}
